﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MixScript.BLL.FI;
using System.Data;
using System.Collections;
using MixScriptWeb.Share;
using MixScript.BLL;
using System.Drawing;


namespace MixScriptWeb.Accounting
{
    public partial class PopUpCoa : System.Web.UI.Page
    {

        private static cCOA oCoa;
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                cCommonWeb common = new cCommonWeb();
                common.FillDropDownList(ddlCurrency, "mst_currency", "currency", "currency", true);
                if (Request.QueryString["EID"] != null)
                {
                    lblTitle.Text = "Chart Of Account (Edit)";
                    txtAccParentNo.Text = Request.QueryString["PID"];
                    txtAccID.Text = Request.QueryString["EID"].Replace(Request.QueryString["PID"], "");
                    LoadUpdateData();
                }
                else
                {
                    lblTitle.Text = "Chart Of Account (Add New)";
                    txtAccParentNo.Text = Request.QueryString["PID"];
                }
            }
        }
        protected void btnSave_Click(Object sender, EventArgs e)
        {
            if (Request.QueryString["EID"] != null)
            {
                Update();
            }
            else
            {
                Save();
            }
            Page.ClientScript.RegisterStartupScript(this.GetType(), "close", "<script language=javascript>window.opener.location.reload(true);self.close();</script>");
        }
        private void LoadUpdateData()
        {
            DataTable dt = new DataTable();
            oCoa = new cCOA();
            string idcoa;
            idcoa = Request.QueryString["EID"];
            if (oCoa.getCoaDetail(ref dt, idcoa))
            {
                txtAccName.Text = (string)dt.Rows[0].ItemArray[1].ToString().TrimEnd();
                if ((string)dt.Rows[0].ItemArray[2] == "C")
                {
                    ddlCurrency.SelectedIndex = 1;
                }
                else
                {
                    ddlCurrency.SelectedIndex = 0;
                }
                ddlProcess.SelectedIndex = Convert.ToInt32(dt.Rows[0].ItemArray[7]) - 1;
                ddlApplication.SelectedIndex = Convert.ToInt32(dt.Rows[0].ItemArray[8]) - 1;
                ddlSubApplication.SelectedIndex = Convert.ToInt32(dt.Rows[0].ItemArray[9]) - 1;

            }
            else { }


        }
        private void Save()
        {
            string parentNo = txtAccParentNo.Text;
            int type = 4;
            if (Request.QueryString["type"] != null)
                type = 3;

            string dbcd;
            int GroupNo, SugGroupNo;
            int.TryParse(txtAccParentNo.Text.Substring(0, 1), out GroupNo);
            int.TryParse(txtAccParentNo.Text.Substring(0, 2), out SugGroupNo);
            if (rbDebit.Checked)
                dbcd = "D";
            else
                dbcd = "C";
            oCoa = new cCOA();
            oCoa.SaveCOA(parentNo + txtAccID.Text, txtAccName.Text, Convert.ToInt32(ddlApplication.SelectedItem.Value),
                Convert.ToInt32(ddlSubApplication.SelectedItem.Value), ddlCurrency.SelectedItem.Value.ToString(),
                dbcd, Convert.ToInt32(ddlProcess.SelectedItem.Value), type, GroupNo, SugGroupNo, true);
        }
        private void Update()
        {
            string parentNo = txtAccParentNo.Text;
            int type = 4;
            if (Request.QueryString["type"] != null)
                type = 3;

            string dbcd;
            int GroupNo, SugGroupNo;
            int.TryParse(txtAccParentNo.Text.Substring(0, 1), out GroupNo);
            int.TryParse(txtAccParentNo.Text.Substring(0, 2), out SugGroupNo);
            if (rbDebit.Checked)
                dbcd = "D";
            else
                dbcd = "C";
            oCoa = new cCOA();
            oCoa.SaveCOA(parentNo + txtAccID.Text.TrimEnd(), txtAccName.Text.TrimEnd(), Convert.ToInt32(ddlApplication.SelectedItem.Value),
                Convert.ToInt32(ddlSubApplication.SelectedItem.Value), ddlCurrency.SelectedItem.Value.ToString(),
                dbcd, Convert.ToInt32(ddlProcess.SelectedItem.Value), type, GroupNo, SugGroupNo, false);
        }
    }

}